//验证手机号
let checkMobileCode = (str) => {
	return !(/^1[3456789]\d{9}$/.test(str));
}

// 验证文本框是否有内容，决定按钮是否可操作
let checkIsEmpty = (str1, str2) => {
	return (str1.trim().length > 0 && str2.trim().length > 0) ? false : true;
}

// 生成指定位数的随机码
let getRndCode = (n) => {
	let strCode = '';
	for (let i = 0; i < n; i++) {
		strCode = strCode + Math.floor(Math.random() * 9);
	}
	return strCode;
}
// 获取年的第几周
let getWeekNumYear = (a, b, c) => {
	var date1 = new Date(a, parseInt(b) - 1, c),
		date2 = new Date(a, 0, 1),
		d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
	return Math.ceil((d + ((date2.getDay() + 1) - 1)) / 7);
}

// 返回带格式的日期
let retFormatDate = (dt) => {
	let _gdt = new Date(Date.parse(dt.replace(/-/g, "/")));
	let _cdt = new Date();
	// 如果是同年
	if (_gdt.getFullYear() == _cdt.getFullYear()) {
		return strAddZero(_gdt.getMonth() + 1) + "月" + strAddZero(_gdt.getDate()) + "日";
	} else { // 不同年
		return _gdt.getFullYear() + "/" + strAddZero(_gdt.getMonth() + 1) + "/" + strAddZero(_gdt.getDate());
	}
}

// 返回带格式的日期
let retFormatTime = (dt) => {
	let _gdt = new Date(Date.parse(dt.replace(/-/g, "/")));
	let _hms = strAddZero(_gdt.getHours()) + ":" + strAddZero(_gdt.getMinutes()) + ":" + strAddZero(_gdt.getSeconds());
	return _hms;
}

// 返回自定义格式的日期
let retCustFormatTime = (dt) => {
	if (dt) {
		let _gdt = new Date(Date.parse(dt.replace(/-/g, "/")));
		let _hms = strAddZero(_gdt.getMonth() + 1) + "." + strAddZero(_gdt.getDate()) + " " + strAddZero(_gdt.getHours()) +
			":" + strAddZero(_gdt.getMinutes());
		return _hms;
	} else {
		return "";
	}

}

// 返回带格式的日期
let retFormatYmdt = (dt) => {
	let _gdt = new Date(Date.parse(dt.replace(/-/g, "/")));
	let _ymd = strAddZero(_gdt.getFullYear()) + "-" + strAddZero(_gdt.getMonth() + 1) + "-" + strAddZero(_gdt.getDate());
	return _ymd;
}

// 返回带格式的当前时间
let retCurFormatYmdhms = () => {
	let _curDate = new Date();
	let _top = strAddZero(_curDate.getFullYear()) + "-" + strAddZero(_curDate.getMonth() + 1) + "-" + strAddZero(_curDate
		.getDate());
	let _bot = strAddZero(_curDate.getHours()) + ":" + strAddZero(_curDate.getMinutes()) + ":" + strAddZero(_curDate.getSeconds());
	return _top + " " + _bot;
}

// 返回补0格式
let strAddZero = (value) => {
	if (Number(value) < 10) {
		return '0' + value;
	}
	return value;
}

// 返回当前时间
let getCurDate = () => {
	let _dt = new Date();
	let _hms = strAddZero(_dt.getHours()) + " : " + strAddZero(_dt.getMinutes()) + " : " + strAddZero(_dt.getSeconds());
	return _hms;
}

// 聊天时格式化时间
let retMsgFormatTime = (timestamp) => {
	// 补全为13位
	var arrTimestamp = (timestamp + '').split('');
	for (var start = 0; start < 13; start++) {
		if (!arrTimestamp[start]) {
			arrTimestamp[start] = '0';
		}
	}
	timestamp = arrTimestamp.join('') * 1;

	var minute = 1000 * 60;
	var hour = minute * 60;
	var day = hour * 24;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime();
	var diffValue = now - timestamp;

	// 如果本地时间反而小于变量时间
	// if (diffValue < 0) {
	// 	return '不久前';
	// }

	// 计算差异时间的量级
	var monthC = diffValue / month;
	var weekC = diffValue / (7 * day);
	var dayC = diffValue / day;
	var hourC = diffValue / hour;
	var minC = diffValue / minute;

	// 数值补0方法
	var zero = function(value) {
		if (value < 10) {
			return '0' + value;
		}
		return value;
	};

	// 使用
	if (monthC > 12) {
		// 超过1年，直接显示年月日
		return (function() {
			var date = new Date(timestamp);
			return date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日';
		})();
	} else if (monthC >= 1) {
		return parseInt(monthC) + "月前";
	} else if (weekC >= 1) {
		return parseInt(weekC) + "周前";
	} else if (dayC >= 1) {
		return parseInt(dayC) + "天前";
	} else if (hourC >= 1) {
		return parseInt(hourC) + "小时前";
	} else if (minC >= 1) {
		return parseInt(minC) + "分钟前";
	}
	return '刚刚';
}

// 导出函数
export {
	checkMobileCode,
	checkIsEmpty,
	getRndCode,
	getWeekNumYear,
	retFormatDate,
	retMsgFormatTime,
	strAddZero,
	retFormatTime,
	retFormatYmdt,
	retCustFormatTime,
	retCurFormatYmdhms
}
